// pages/orderList/orderList.js
const app = getApp();
import api from '../../api/index.js';
const { request } = require('../../utils/wx-api.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    topNav:["全部","待付款","已付款","已发货","待评价"],
    idx:0,
    orderList:'',
    lists:'',
    hasOrder:false
  },
  tap(e) {
    let { idx } = e.currentTarget.dataset;
    let lists = [];
    let { orderList } = this.data;
    for (let i = 0; i < orderList.length; i++) {
      if (orderList[i].order_status==idx-1 || orderList[i].order_status==idx) {
        lists.push(orderList[i]);
      }
    }
    if(!lists.length) {
      this.setData({
        hasOrder:false
      })
    }else {
      this.setData({
        hasOrder: true
      })
    }
    this.setData({
      idx,
      lists
    })
  },
  delOrder(e){
    console.log(e);
    let that = this;
    let orderId = e.currentTarget.dataset.id;
    let {index} = e.currentTarget.dataset;
    let {orderList} = this.data;
    let {lists} = this.data;
    wx.showModal({
      title: '提示',
      content: '要删除此订单？',
      success(res){
        if(res.confirm){
          request({url:api.orderCancel},{orderId},"GET",{'X-Nideshop-Token': app.globalData.token})
          .then(result=>{
            console.log(result);
            orderList.splice(index,1);
            lists.splice(index,1);
            that.setData({
              orderList,
              lists
            })
            if (!orderList.length) {
              that.setData({
                hasOrder:false
              })
            }
            wx.showToast({
              title: result.data.data
            })
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    request({url:api.orderList},{},"GET",{'X-Nideshop-Token': app.globalData.token})
    .then(res=>{
      console.log('order',res);
      let orderList = res.data.data.data;
      let lists = orderList;
      if(lists.length) {
        this.setData({
          hasOrder:true
        })
      }
      this.setData({
        orderList,
        lists
      })
    })
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})